### Compute popularity index

# This one takes a long time...

# Load network data --------
list_networks_strong <- readRDS("intermediary_outputs/list_networks_clean.RData")
data <- readRDS("intermediary_outputs/data_out_3.Rds")

# Load supporting functions
source("04a_supporting_functions_en.R", encoding = "UTF-8")

# # Transform network into interactions network, using the intersection approach:
# matrix_networks <- lapply(list_networks, as.matrix)
# 
# for (i in 1:length(matrix_networks)) {
#   diag(matrix_networks[[i]]) <- 0        
# }
# 
# matrices_list_strong <- lapply(matrix_networks, sna::symmetrize, "strong")
# 
# list_networks_strong <- lapply(matrices_list_strong , network, directed = F)
# 
# ## Correcting verteces names -----
# for (i in 1:length(list_networks)) {
#   propernames <- network.vertex.names(list_networks[[i]])
#   network.vertex.names(list_networks_strong[[i]]) <- propernames
# }
# 
# attr_list <- c("class_id","school_id", "male_imputed", 
#                "negro_na", "race", "race_simple", "id_interview", 
#                "skin_group_2")
# 
# ## Inputing attributes for strong networks -------
# for (var in attr_list) {
#   print(var)
#   list_networks_strong <- lapply(list_networks_strong, function(x) 
#     input_attr_network(some_network = x, some_attr = var))  
# }
# 

# Compute social status across all races --------
final_scores_2 <- do.call(rbind, 
                          lapply(list_networks_strong, popularity_index))

data$ssi_all_race_2 <- NA

for (i in unique(final_scores_2[,1])) {
  data$ssi_all_race_2[which(data$id_student == i)] <- 
    final_scores_2[which(final_scores_2[,1] == i),2]  
}

# Standardize measure:
selection_criteria <- which(data$flag_select_sample == 0 & 
                              is.na(data$id_interview) == F)

data$ssi_all_race_2_std <- std_vector(data$ssi_all_race_2)
data$ssi_all_race_2_std[which(data$flag_select_sample == 1 | 
                                is.na(data$id_interview) == T)] <- NA

## Computing number of friends ------
result_get_friends_2 <- lapply(list_networks_strong, get_friends)
list_friends_2 <- list()

for (i in 1:length(result_get_friends_2)) {
  list_networks_strong[[i]] <- result_get_friends_2[[i]][[1]]
  list_friends_2[[i]] <- result_get_friends_2[[i]][[2]]
  
}

## Inputing values into matrix: ------
list_friends_2 <- as.data.table(do.call(rbind, list_friends_2))  

colnames(list_friends_2) <- c("id_student", "friends_2", "white_friends_2", 
                              "black_friends_2", "brown_friends_2", 
                              "other_friends_2", 
                              "nonwhite_friends_2", "diff_race_friends_2", 
                              "diff_nw_friends_2", "same_race_friends_2", 
                              "same_race_nw_friends_2")

data <- merge.data.table(data, list_friends_2, by = "id_student", all.x = T)

# Compute share of each race friends: ---------
data$sh_wh_fr_2 <- data$white_friends_2/data$friends_2
data$sh_nw_fr_2 <- (data$black_friends_2 + data$brown_friends_2)/data$friends_2
data$sh_br_fr_2 <- data$brown_friends_2/data$friends_2
data$sh_bl_fr_2 <- data$black_friends_2/data$friends_2

## Compute Social Status within race: -------------------
ssi_same_race_simple_2 <- unlist(sapply(list_networks_strong, 
                                        ssi_net, 
                                        "race_simple"))

data$ssi_same_race_simple_2 <- NA

for (i in unique(as.numeric(names(ssi_same_race_simple_2)))) {
  data$ssi_same_race_simple_2[which(data$id_student == i)] <- 
    ssi_same_race_simple_2[
      which(as.numeric(names(ssi_same_race_simple_2)) == i)]
}

# Standardize
data$ssi_same_race_simple_2_std <- std_vector(data$ssi_same_race_simple_2)
data$ssi_same_race_simple_2_std[which(data$flag_select_sample == 1 | 
                                        is.na(data$id_interview) == T)] <- NA


# Compute the social status for the white vs. nonwhite (with NA for others):
ssi_same_negro_na_2 <- unlist(sapply(list_networks_strong, 
                                     ssi_net, 
                                     "negro_na"))

data$ssi_same_negro_na_2 <- NA

for (i in unique(as.numeric(names(ssi_same_negro_na_2)))) {
  data$ssi_same_negro_na_2[which(data$id_student == i)] <- 
    ssi_same_negro_na_2[which(as.numeric(names(ssi_same_negro_na_2))== i)]
}

# Standardize
data$ssi_same_negro_na_2_std <- std_vector(data$ssi_same_negro_na_2)
data$ssi_same_negro_na_2_std[which(data$flag_select_sample == 1 | 
                                     is.na(data$id_interview) == T)] <- NA

## Compute Social Status within same skin group: ----------
ssi_same_skin_group_2 <- unlist(sapply(list_networks_strong, 
                                       ssi_net,
                                       "skin_group_2"))

data$ssi_same_skin_group_2 <- NA

for (i in unique(as.numeric(names(ssi_same_skin_group_2)))) {
  data$ssi_same_skin_group_2[which(data$id_student == i)] <- 
    ssi_same_skin_group_2[which(as.numeric(names(ssi_same_skin_group_2))== i)]
}

# Standardize
data$ssi_same_skin_group_2_std <- std_vector(data$ssi_same_skin_group_2)
data$ssi_same_skin_group_2_std[which(data$flag_select_sample == 1 | 
                                       is.na(data$id_interview) == T)] <- NA

# Compute social status index based on other race (nonwhite vs white only)  
ssi_other_race_negro_na_2 <- unlist(sapply(list_networks_strong, ssi_net_other_2_race, "negro_na"))
data$ssi_other_race_negro_na_2 <- NA

for (i in unique(as.numeric(names(ssi_other_race_negro_na_2)))) {
  data$ssi_other_race_negro_na_2[which(data$id_student == i)] <- 
    ssi_other_race_negro_na_2[
      which(as.numeric(names(ssi_other_race_negro_na_2))== i)]
}

# Standardize measure
data$ssi_other_race_negro_na_2_std <- 
  std_vector(data$ssi_other_race_negro_na_2)
data$ssi_other_race_negro_na_2_std[which(data$flag_select_sample == 1 | 
                                           is.na(data$id_interview) == T)] <- NA

# Dark against light skin color:
ssi_other_race_skin_group_2 <- unlist(sapply(list_networks_strong, 
                                             ssi_net_other_2_race, 
                                             "skin_group_2"))
data$ssi_other_race_skin_group_2 <- NA

for (i in unique(as.numeric(names(ssi_other_race_skin_group_2)))) {
  data$ssi_other_race_skin_group_2[which(data$id_student == i)] <- 
    ssi_other_race_skin_group_2[
      which(as.numeric(names(ssi_other_race_skin_group_2))== i)]
}

# Standardize measure
data$ssi_other_race_skin_group_2_std <- 
  std_vector(data$ssi_other_race_skin_group_2)

data$ssi_other_race_skin_group_2_std[
  which(data$flag_select_sample == 1 | is.na(data$id_interview) == T)] <- NA

# Compute Social Status for other races (white, blacks, and browns) 
ssi_other_race_simple_2 <- unlist(sapply(list_networks_strong, 
                                         ssi_net_other_3_race, 
                                         "race_simple"))
data$ssi_other_race_simple_2 <- NA

for (i in unique(as.numeric(names(ssi_other_race_simple_2)))) {
  data$ssi_other_race_simple_2[which(data$id_student == i)] <- 
    ssi_other_race_simple_2[
      which(as.numeric(names(ssi_other_race_simple_2))== i)]
}

# Standardize measure
data$ssi_other_race_simple_2_std <- std_vector(data$ssi_other_race_simple_2)
data$ssi_other_race_simple_2_std[which(data$flag_select_sample == 1 | 
                                         is.na(data$id_interview) == T)] <- NA


## Other race social status among skin color groups
ssi_other_race_group_2 <- unlist(sapply(list_networks_strong, 
                                        ssi_net_other_3_race, 
                                        "skin_group"))
data$ssi_other_race_group_2 <- NA

for (i in unique(as.numeric(names(ssi_other_race_group_2)))) {
  data$ssi_other_race_group_2[which(data$id_student == i)] <- 
    ssi_other_race_group_2[
      which(as.numeric(names(ssi_other_race_group_2))== i)]
}

# Standardize measure
data$ssi_other_race_group_2_std <- std_vector(data$ssi_other_race_group_2)
data$ssi_other_race_group_2_std[which(data$flag_select_sample == 1 | 
                                        is.na(data$id_interview) == T)] <- NA


## Input some SSI measures into networks:
for (j in 1:length(list_networks_strong)) {
  # j = 3
  some_network <- list_networks_strong[[j]]
  names <- as.numeric(some_network %v% "vertex.names")
  ssi_same_negro_na_correct_order <- rep(NA, network.size(some_network))
  ssi_other_race_negro_na_2_order <- rep(NA, network.size(some_network))
  ssi_correct_order <- rep(NA, network.size(some_network))

  for (i in 1:length(names)) {
    # i = 4
    ssi_same_negro_na_correct_order[i] <- ssi_same_negro_na_2[
      which(as.numeric(names(ssi_same_negro_na_2)) == names[i])]
    
    ssi_other_race_negro_na_2_order[i] <- ssi_other_race_negro_na_2[
      which(as.numeric(names(ssi_other_race_negro_na_2)) == names[i])]
    
    ssi_correct_order[i] <- 
      final_scores_2[which(final_scores_2[,1] == names[i]),2]
  }

  some_network %v% "ssi_same_race_negro_na" <- 
    ssi_same_negro_na_correct_order
  
  some_network %v% "ssi_other_race_negro_na_2" <- 
    ssi_other_race_negro_na_2_order
  
  some_network %v% "ssi_all_race" <- 
    ssi_correct_order
  
  list_networks_strong[[j]] <- some_network
}

## Friends' characteristics:
data$avg_skin_fr_2 <- NA

for (j in 1:length(list_networks_strong)) {
  # j = 4
  my_class_id <- as.numeric(network.vertex.names(list_networks_strong[[j]]))
  class_size <- length(my_class_id)
  
  for (i in 1:class_size) {
    # i = 1
    myneigh <- get.neighborhood(list_networks_strong[[j]], v = i)
    
    myid    <- 
      as.numeric(network::network.vertex.names(list_networks_strong[[j]])[i])
    
    myrace  <- network::get.vertex.attribute(
      list_networks_strong[[j]], attrname = "race_simple")[i]
    
    my_friends_race <- network::get.vertex.attribute(
      list_networks_strong[[j]], attrname = "race_simple")[myneigh]
    
    my_friends <- length(myneigh)
    
    my_friends_id <- as.numeric(
      network::network.vertex.names(list_networks_strong[[j]])[myneigh])
    
    # Input the number of friends in the data frame
    data$avg_skin_fr_2[which(data$id_student == myid)] <-
      mean(data$skin_color[which(data$id_student %in% my_friends_id)], 
           na.rm = T)
  }  
}

saveRDS(data, "intermediary_outputs/data_out_4.Rds")
saveRDS(list_networks_strong, "intermediary_outputs/list_networks_strong_out_4.Rds")
